(function() {
    angular.module('app').directive('echartsDrawCanvas', ['$rootScope', function($rootScope) {
        return {
            restrict: 'A',
            scope: {
                echartsLink: '&'
            },
            link: function(scope, iElement, iAttrs) {

                scope.chart = echarts.init(iElement[0]);

                var clearFunc = $rootScope.$on('echart_draw_EV', function(e, data) {
                    // console.log('注意，要画图了...');

                    // console.log('io-------');
                    // console.log(data);
                    scope.chart.clear();
                    scope.chart.showLoading();
                    data = chartPieResize(data);
                    scope.chart.setOption(data);
                    scope.chart.hideLoading();
                    if (scope.echartsLink) {
                        // console.log(typeof scope.echartsLink);
                        scope.chart.on('click', scope.echartsLink());
                    }
                });

                function chartPieResize(data) {
                    if (data.series[0].type == 'pie') {
                        var width = $(iElement).width();
                        var height = $(iElement).height();
                        // console.log((width / height).toFixed(2));
                        if ((width / height).toFixed(2) > 1.0) {
                            data.series[0].radius = '48%';
                        }

                        if($(window).width()<415){
                            data.series[0].radius = '40%';
                        }

                    }
                    // console.log(data);
                    return data;

                }

                scope.$on('$destroy', function() {
                    clearFunc();
                    scope.chart.off('click');
                });
            }
        };
    }]);
})();
